//
//  ANFindTool.m
//  Ants
//
//  Created by ZHM on 16/6/30.
//  Copyright © 2016年 ZHM. All rights reserved.
//

#import "ANFindTool.h"
#import "ANOptionsTableViewController.h"
#import "ANOptionTableView.h"
#import "ANOptionTableViewSub.h"
#import "ANOptionTableViewSubChild.h"
#import "ANOptionFootView.h"

// -1 > NotTabViewRowIndex
#define NotTabViewRowIndex -10

@interface ANFindTool ()<ANOptionsTableViewControllerDelegate,ANOptionFootViewDelegate>

@property (nonatomic, assign) NSInteger indexRomChangecityBtn;
@property (nonatomic, weak) ANFindToolBtn *cityBtn;
@property (nonatomic, weak) ANFindToolBtn *categoryBtn;
@property (nonatomic, weak) ANFindToolBtn *sortBtn;
@property (nonatomic, weak) ANFindToolBtn * selectedBtn;
@property (nonatomic, strong) ANOptionsTableViewController *optionTableView;
@property (nonatomic, strong) ANOptionsTableViewController *optionTableView1;
@property (nonatomic, strong) ANOptionsTableViewController *optionTableView2;
@property (nonatomic, strong) ANOptionsTableViewController *optionTableViewSub;
@property (nonatomic, strong) ANOptionsTableViewController *optionTableViewSubChild;
@property (nonatomic, strong) NSMutableArray *btns;
@property (nonatomic, strong) NSMutableArray *divides;
@property (nonatomic, weak) ANOptionTableView *optionView;
@property (nonatomic, weak) ANOptionTableView *optionView1;
@property (nonatomic, weak) ANOptionTableView *optionView2;
@property (nonatomic, weak) ANOptionTableViewSub *optionViewSub;
@property (nonatomic, weak) ANOptionTableViewSubChild *optionViewSubChild;
@property (nonatomic, strong) ANOptionFootView  *footView;
@property (nonatomic, weak) UIToolbar *toolbar;
@property (nonatomic, weak) UIButton *coverView;

@property (nonatomic, strong) NSMutableArray *productCategory;
@property (nonatomic, strong) NSArray *sortBy;

@property(nonatomic,strong)NSArray *provinces;
@property(nonatomic,strong)NSArray *citys;
@property(nonatomic,strong)NSArray *areas;

@end

@implementation ANFindTool

- (NSArray *)provinces {
    if (!_provinces) {
        NSString *addressPath = [[NSBundle mainBundle] pathForResource:@"address" ofType:@"plist"];
        NSMutableDictionary *dict = [[NSMutableDictionary alloc]initWithContentsOfFile:addressPath];
        _provinces = [dict objectForKey:@"address"];
    }
    return _provinces;
}
- (void)changecityBtnTitleWithString:(NSString *)title {
    [_cityBtn setTitle:title forState:UIControlStateNormal];
    _indexRomChangecityBtn = NotTabViewRowIndex;
    if (_optionView) {
        _optionTableView.indexRom = NotTabViewRowIndex;
        [_optionTableView.tableView reloadData];
        [self cancelOptionViewWithFindToolBtn:_selectedBtn];
        if (_optionViewSub) [_optionViewSub removeFromSuperview];
        if (_optionViewSubChild) [_optionViewSubChild removeFromSuperview];
    }
}
- (NSArray *)citys {
    if (!_citys) {
        _citys = [self.provinces[_optionTableView.indexRom - 1] objectForKey:@"sub"];
    }
    return _citys;
}

- (NSArray *)areas {
    if (!_areas) {
        _areas = [self.citys[_optionTableViewSub.indexRom - 1] objectForKey:@"sub"];
    }
    return _areas;
}
- (NSArray *)sortBy {
    if (!_sortBy) {
        NSString *sortByPath = [[NSBundle mainBundle] pathForResource:@"sortBy" ofType:@"plist"];
         NSMutableDictionary *sortByDic = [[NSMutableDictionary alloc]initWithContentsOfFile:sortByPath];
        _sortBy = [sortByDic objectForKey:@"sortBy"];
    }
    return  _sortBy;
}
- (ANOptionFootView *)footView {
    if (!_footView) {
        ANOptionFootView *footView = [ANOptionFootView footView];
        footView.delegate = self;
        _footView = footView;
    }
    return _footView;
}
//添加按钮-添加新的
- (void)updateTableViewWithNewCategory:(ANBDProductCategory *)productCategory {
    [self.optionTableView1.arrayCells addObject:productCategory.title];
    ANLog(@"%@=ID:%@",productCategory.title,productCategory.bmObject);
    //更新
    [_productCategory addObject:productCategory];
    if (self.optionTableView1.arrayCells.count <= 7) {
        CGRect frame = self.optionView1.frame;
        frame.size.height += OptionsTableViewCellHeight;
        self.optionView1.frame = frame;
        //[_optionTableView1.tableView setContentOffset:CGPointMake(0, _optionTableView1.tableView.contentSize.height - _optionTableView1.tableView.bounds.size.height)animated:NO];
    }
    [self.optionTableView1.tableView reloadData];
}
- (NSMutableArray *)productCategory {
    if (!_productCategory) {
        _productCategory = [NSMutableArray array];
        //本地获取
        NSArray *arrayBD = [ANBDProductCategory MR_findAllInContext:sharedWebService.defaultContext];
        ANLog(@"本地获取array:%ld",(unsigned long)arrayBD.count);
        self.optionTableView1.arrayCells = [self ANBDProductCategoryToString:arrayBD];
        [self.optionTableView1.arrayCells insertObject:@"所有商品" atIndex:0];
        
        [_productCategory setArray: arrayBD];
        ANLog(@"array:%ld",(unsigned long)self.optionTableView1.arrayCells.count);
        [self.optionTableView1.tableView reloadData];
        //网络获取再更新
        [sharedWebService fetchProductCategoryCompletion:^(NSArray *productCategorys, NSError *error) {
            if (!error) {
                //更新内存
                [self.optionTableView1.arrayCells setArray: [self ANBDProductCategoryToString:productCategorys]];
                [self.optionTableView1.arrayCells insertObject:@"所有商品" atIndex:0];
                [_productCategory setArray: productCategorys];
                [self.optionTableView1.tableView reloadData];
            }
        }];
    }
    return _productCategory;
}

- (NSMutableArray *)ANBDProductCategoryToString:(NSArray *)arrayANDB {
    NSMutableArray *arrayM = [NSMutableArray array];
    [arrayANDB enumerateObjectsUsingBlock:^(ANBDProductCategory *obj, NSUInteger idx, BOOL * _Nonnull stop) {
        NSString *categoryName = obj.title;
        [arrayM addObject:categoryName];
    }];
    return arrayM;
}

- (NSArray *)NSDictionarysToStrings:(NSArray *)dicts {
    NSMutableArray *strs = [NSMutableArray array];
    for (NSDictionary *dict in dicts) {
        NSString *str = [dict objectForKey:@"name"];
        [strs addObject:str];
    }
    return strs;
}
- (ANOptionTableView *)optionView {
    if (!_optionView) {
        _optionView = [ANOptionTableView optionTableViewWithRect:CGRectMake(0, CGRectGetMaxY(self.frame), self.width, OptionsTableViewCellHeight * 8) toView:self.superview];
    }
    return _optionView;
}
- (ANOptionTableView *)optionView1{
    CGFloat height = OptionsTableViewCellHeight * 9;
    if (_optionTableView1.arrayCells.count < 8) {
        height = OptionsTableViewCellHeight * (_optionTableView1.arrayCells.count + 1);
    }
    if (!_optionView1) {
        _optionView1 = [ANOptionTableView optionTableViewWithRect:CGRectMake(0, CGRectGetMaxY(self.frame), self.width, height) toView:self.superview];
    }
    return _optionView1;
}
- (ANOptionTableView *)optionView2 {
    CGFloat height = OptionsTableViewCellHeight * 8;
    if (_optionTableView2.arrayCells.count < 8) {
        height = OptionsTableViewCellHeight *_optionTableView2.arrayCells.count;
    }
    if (!_optionView2) {
        _optionView2 = [ANOptionTableView optionTableViewWithRect:CGRectMake(0, CGRectGetMaxY(self.frame), self.width, height) toView:self.superview];
    }
    return _optionView2;
}
- (ANOptionTableViewSub *)optionViewSub {
    if (!_optionViewSub) {
        ANOptionTableViewSub *optionTableViewSub = [ANOptionTableViewSub optionTableViewWithRect:CGRectMake(ANKeyWindow.width/3, 0, ANKeyWindow.width/3*2, _optionView.height) toView:_optionView];
        _optionViewSub = optionTableViewSub;
    }
    return _optionViewSub;
}
- (ANOptionTableViewSubChild *)optionViewSubChild {
    if (!_optionViewSubChild) {
        ANOptionTableViewSubChild *optionTableViewSubChild = [ANOptionTableViewSubChild optionTableViewWithRect:CGRectMake(_optionViewSub.width/2, 0, _optionViewSub.width/2, _optionViewSub.height) toView:_optionViewSub];
        _optionViewSubChild = optionTableViewSubChild;
    }
    return _optionViewSubChild;
}
- (ANOptionsTableViewController *)optionTableView {
    if (!_optionTableView) {
        _optionTableView = [[ANOptionsTableViewController alloc] initWithStyle:UITableViewStylePlain];
        _optionTableView.CellBackgroundColor = ProvincesBackground;
        _optionTableView.indexRom = _indexRomChangecityBtn;
        if ([_selectedBtn.name isEqualToString:@"分类"]) {
            _optionTableView.IsEditRow = YES;
        }else {
            _optionTableView.IsEditRow = NO;
        }
        _optionTableView.delegate = self;
    }
    return _optionTableView;
}
- (ANOptionsTableViewController *)optionTableView1 {
    if (!_optionTableView1) {
        _optionTableView1 = [[ANOptionsTableViewController alloc] initWithStyle:UITableViewStylePlain];
        _optionTableView1.CellBackgroundColor = ProvincesBackground;
        if ([_selectedBtn.name isEqualToString:@"分类"]) {
            _optionTableView1.IsEditRow = YES;
        }else {
            _optionTableView1.IsEditRow = NO;
        }
        _optionTableView1.delegate = self;
    }
    return _optionTableView1;
}
- (ANOptionsTableViewController *)optionTableView2 {
    if (!_optionTableView2) {
        _optionTableView2 = [[ANOptionsTableViewController alloc] initWithStyle:UITableViewStylePlain];
        _optionTableView2.CellBackgroundColor = ProvincesBackground;
        if ([_selectedBtn.name isEqualToString:@"分类"]) {
            _optionTableView2.IsEditRow = YES;
        }else {
            _optionTableView2.IsEditRow = NO;
        }
        _optionTableView2.delegate = self;
    }
    return _optionTableView2;
}
- (ANOptionsTableViewController *)optionTableViewSub {
    if (!_optionTableViewSub) {
        _optionTableViewSub = [[ANOptionsTableViewController alloc] initWithStyle:UITableViewStylePlain];
        _optionTableViewSub.CellBackgroundColor = CitysBackground;
        _optionTableViewSub.IsEditRow = NO;
        _optionTableViewSub.delegate = self;
    }
    return _optionTableViewSub;
}
- (ANOptionsTableViewController *)optionTableViewSubChild {
    if (!_optionTableViewSubChild) {
        _optionTableViewSubChild = [[ANOptionsTableViewController alloc] initWithStyle:UITableViewStylePlain];
        _optionTableViewSubChild.CellBackgroundColor = AreaBackground;
        _optionTableViewSubChild.IsEditRow = NO;
        _optionTableViewSubChild.delegate = self;
    }
    return _optionTableViewSubChild;
}
- (NSMutableArray *)btns {
    if (!_btns) {
        _btns = [NSMutableArray array];
    }
    return _btns;
}
- (NSMutableArray *)divides {
    if (!_divides) {
        _divides = [NSMutableArray array];
    }
    return _divides;
}
+ (instancetype)showWithTableView:(UITableView *)tableView{
    ANFindTool *barTool = [[ANFindTool alloc] init];
    //barTool.backgroundColor = [[UIColor colorWithRed:246.0/255 green:246.0/255 blue:246.0/255 alpha:1] colorWithAlphaComponent:0.96];
    
    barTool.frame = CGRectMake(0, 64, ANKeyWindow.width, 35);
    tableView.frame = CGRectMake(0, 35, ANKeyWindow.width, ANKeyWindow.height);
    barTool.tableView = tableView;
    return barTool;
}
- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        [self addUpAllChildView];
        self.userInteractionEnabled = YES;
        //self.image = [UIImage imageWithStretchableName:@"timeline_retweet_background"];
    }
    return self;
}
- (void)addUpAllChildView {
    UIToolbar *toolbar = [[UIToolbar alloc] init];
    toolbar.barStyle = UIBarStyleDefault;
    _toolbar = toolbar;
    [self addSubview:toolbar];
    
    
    _cityBtn = [self setUpOneButtonWithTitle:@"城市"];
    _cityBtn.name = @"城市";
    _categoryBtn = [self setUpOneButtonWithTitle:@"分类"];
    _categoryBtn.name = @"分类";
    _sortBtn = [self setUpOneButtonWithTitle:@"排序"];
    _sortBtn.name = @"排序";
    [_cityBtn addTarget:self action:@selector(rotatingWithBtn:) forControlEvents:UIControlEventTouchUpInside];
    [_categoryBtn addTarget:self action:@selector(rotatingWithBtn:) forControlEvents:UIControlEventTouchUpInside];
    [_sortBtn addTarget:self action:@selector(rotatingWithBtn:) forControlEvents:UIControlEventTouchUpInside];

    //分割线
//    for (int i = 0; i < 2; i++) {
//        UIImageView *divider = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"timeline_card_bottom_line_highlighted"]];
//        [self addSubview:divider];
//        [self.divides addObject:divider];
//    }
}
//清除选项表
- (void)cancelOptionViewWithFindToolBtn:(ANFindToolBtn *)btn{
    btn.selected = NO;
    //移除捆绑控制器
    switch (_selectedBtn.tag) {
        case 100:
            [_optionView hideOptionViewAnimation:YES];
            break;
        case 101:
            [_optionView1 hideOptionViewAnimation:YES];
            break;
        case 102:
            [_optionView2 hideOptionViewAnimation:YES];
            break;
    }
    [self hideCoverView];
    //隐藏footView输入框
    [(ANOptionFootView*)_optionTableView.tableView.tableFooterView hideInputViewAnimation:YES];
    _selectedBtn = nil;
}

- (void)rotatingWithBtn:(ANFindToolBtn *)btn{
    if (_selectedBtn == nil) {
        [self showCoverView];
    }
    if (_selectedBtn.tag != btn.tag && _selectedBtn) {
        _selectedBtn.imageView.transform = CGAffineTransformIdentity;
        _selectedBtn.selected = NO;
        switch (_selectedBtn.tag) {
            case 100:
                [_optionView hideOptionViewAnimation:NO];
                break;
            case 101:
                [_optionView1 hideOptionViewAnimation:NO];
                break;
            case 102:
                [_optionView2 hideOptionViewAnimation:NO];
                break;
        }
    }
    if (btn.isSelected) {
        [self cancelOptionViewWithFindToolBtn:btn];
    } else {
        btn.selected = YES;
        _selectedBtn = btn;
        switch (btn.tag) {
            case 100:
                //打开捆绑控制器
                if (self.optionTableView.arrayCells.count > 0) {
                    [self.optionView showOptionView];
                }else {
                    [self.optionTableView.arrayCells setArray: [self NSDictionarysToStrings:self.provinces]];
                    [self.optionTableView.arrayCells insertObject:@"全国" atIndex:0];
                    [self.optionView showOptionView];
                    self.optionView.contentView = self.optionTableView.view;
                    self.optionTableView.tableView.tableFooterView = nil;
                }
                break;
            case 101:
                if (_optionView1.contentView == self.optionTableView1.view) {
                    [self.optionView1 showOptionView];
                } else {
                    [self productCategory];
                    
                    self.optionTableView1.tableView.tableFooterView = self.footView;
                    self.optionView1.contentView = self.optionTableView1.view;
                    [self.optionView1 showOptionView];
                }
                break;
            case 102:
                if (self.optionTableView2.arrayCells.count > 0) {
                    [self.optionView2 showOptionView];
                } else {
                    [self.optionTableView2.arrayCells setArray:self.sortBy];
                    self.optionView2.contentView = self.optionTableView2.view;
                    [self.optionView2 showOptionView];
                }
                break;
        }
        
        //实现按钮图片的旋转
        [UIView animateWithDuration:ANFindToolAnimationTime animations:^{
            //判断形变属性transform 是否被修改
            if (CGAffineTransformIsIdentity(btn.imageView.transform)) {
                btn.imageView.transform = CGAffineTransformMakeRotation(M_PI/2);
            }else {
                //恢复形变属性transform 初始值
                btn.imageView.transform = CGAffineTransformIdentity;
            }
        }];
    }
}
- (void)reloadTableViewVC:(ANOptionsTableViewController *)tableViewVC withRow:(NSInteger)rom {
    NSIndexPath *indexPath=[NSIndexPath indexPathForRow:rom inSection:0];
    NSArray *indexArray=[NSArray arrayWithObject:indexPath];
    [tableViewVC.tableView reloadRowsAtIndexPaths:indexArray withRowAnimation:UITableViewRowAnimationAutomatic];
}

- (void)didSelectSection:(NSInteger)section stringArray:(NSArray*)array{
    if ([self.delegate respondsToSelector:@selector(findTool:didSelectSection:stringArray:)]) {
        [self.delegate findTool:self didSelectSection:section stringArray:array];
    }
}

#pragma mark -- tableView 点击Cell
- (void)opitonsTableView:(ANOptionsTableViewController *)opitonsTableView didSelectCellContent:(NSString *)content rom:(NSUInteger)rom {
    
    if (rom == 0) {
        switch (_selectedBtn.tag) {
            case 100:
                if (opitonsTableView == _optionTableView) {
                    [_selectedBtn setTitle:content forState:UIControlStateNormal];
                    [self reloadTableViewVC:_optionTableView withRow:0];
                    [_optionViewSub removeFromSuperview];
                    [_optionViewSubChild removeFromSuperview];
                    ANLog(@"全国-%@",content);
                    [self didSelectSection:0 stringArray:@[content]];
                }else if (opitonsTableView == _optionTableViewSub){
                    [_selectedBtn setTitle:_optionTableView.arrayCells[_optionTableView.indexRom] forState:UIControlStateNormal];
                    [self reloadTableViewVC:_optionTableViewSub withRow:0];
                    [_optionViewSub removeFromSuperview];
                    [_optionViewSubChild removeFromSuperview];
                    ANLog(@"全省-%@",_optionTableView.arrayCells[_optionTableView.indexRom]);
                    NSString *province = _optionTableView.arrayCells[_optionTableView.indexRom];
                    if ([province isEqualToString:@"北京"] || [province isEqualToString:@"天津"] || [province isEqualToString:@"上海"] || [province isEqualToString:@"重庆"]) {
                        province = [NSString stringWithFormat:@"%@市",province];
                    }
                    [self didSelectSection:0 stringArray:@[province]];
                }else if (opitonsTableView == _optionTableViewSubChild){
                    [_selectedBtn setTitle:_optionTableViewSub.arrayCells[_optionTableViewSub.indexRom] forState:UIControlStateNormal];
                    [self reloadTableViewVC:_optionTableViewSubChild withRow:0];
                    [_optionViewSubChild removeFromSuperview];
                    ANLog(@"全市-%@",_optionTableViewSub.arrayCells[_optionTableViewSub.indexRom]);
                    [self didSelectSection:0 stringArray:@[_optionTableViewSub.arrayCells[_optionTableViewSub.indexRom]]];
                }
                
                break;
            case 101:
                [_selectedBtn setTitle:content forState:UIControlStateNormal];
                [self reloadTableViewVC:_optionTableView1 withRow:0];
                ANLog(@"默认类-%@",content);
                [self didSelectSection:1 stringArray:@[content]];
                break;
            case 102:
                [_selectedBtn setTitle:content forState:UIControlStateNormal];
                [self reloadTableViewVC:_optionTableView2 withRow:0];
                ANLog(@"默认排序-%@",content);
                NSNumber *indexSort = [NSNumber numberWithInteger:rom];
                [self didSelectSection:2 stringArray:@[indexSort]];
                break;
        }
        
    } else {
        switch (_selectedBtn.tag) {
            case 100:
                if (opitonsTableView == _optionTableView) {
                    _citys = nil;
                    [_optionViewSubChild removeFromSuperview];
                    _optionViewSubChild = nil;
                    if (self.citys.count > 0) {
                        [self.optionTableViewSub.arrayCells setArray: [self NSDictionarysToStrings:self.citys]];
                        [self.optionTableViewSub.arrayCells insertObject:[NSString stringWithFormat:@"全%@",_optionTableView.arrayCells[_optionTableView.indexRom]] atIndex:0];
                        [self.optionViewSub showOptionView];
                        self.optionViewSub.contentView = self.optionTableViewSub.view;
                        _optionTableViewSub.indexRom = -1;
                        [_optionTableViewSub.tableView reloadData];
                        [_optionTableView.tableView reloadData];
                        return;
                    }
                } else if (opitonsTableView == _optionTableViewSub){
                    
                    _areas = nil;
                    if (self.areas.count > 0) {
                        [self.optionTableViewSubChild.arrayCells setArray:self.areas];
                        [self.optionTableViewSubChild.arrayCells insertObject:[NSString stringWithFormat:@"全%@",_optionTableViewSub.arrayCells[_optionTableViewSub.indexRom]] atIndex:0];
                        [self.optionViewSubChild showOptionView];
                        self.optionViewSubChild.contentView = self.optionTableViewSubChild.view;
                        _optionTableViewSubChild.indexRom = -1;
                        [_optionTableViewSubChild.tableView reloadData];
                        [_optionTableViewSub.tableView reloadData];
                        return;
                    }
                }else if (opitonsTableView == _optionTableViewSubChild) {
                    [self reloadTableViewVC:_optionTableViewSubChild withRow:rom];
                    ANLog(@"区县-%@--%@--%@",_optionTableView.arrayCells[_optionTableView.indexRom],_optionTableViewSub.arrayCells[_optionTableViewSub.indexRom],content);
                    [self didSelectSection:0 stringArray:@[_optionTableViewSub.arrayCells[_optionTableViewSub.indexRom],content]];
                }
                break;
            case 101:
                [_optionTableView1.tableView reloadData];
                ANLog(@"选类-%@",content);
                [self didSelectSection:1 stringArray:@[_productCategory[rom - 1]]];
                break;
            case 102:
                [_optionTableView2.tableView reloadData];
                ANLog(@"选排序-%@",content);
                NSNumber *indexSort = [NSNumber numberWithInteger:rom];
                [self didSelectSection:2 stringArray:@[indexSort]];
                break;
        }
        //修改按钮名字
        [_selectedBtn setTitle:content forState:UIControlStateNormal];
    }
    //隐藏表格
    [self cancelOptionViewWithFindToolBtn:_selectedBtn];
}
#pragma mark--CELL 删除按钮
- (void)opitonsTableView:(ANOptionsTableViewController *)opitonsTableView deleCellforRow:(NSInteger)row completion:(void (^)(BOOL, NSError *))completion{
    ANBDProductCategory *obj = _productCategory[row-1];
    BmobObject *bmObject = (BmobObject*)obj.bmObject;
    [sharedWebService deleProductCategoryWithObjectId:bmObject.objectId completion:^(BOOL succeeded, NSError *error) {
        if (error) {
            ANLog(@"删除数据出错:%@",error);
            BLOCK_SAFE_ASY_RUN_MainQueue(completion, NO, error);
        } else {
            ANLog(@"删除数据成功:%@",[bmObject objectForKey:@"title"]);
            ANLog(@"内存数据删除:%@",opitonsTableView.arrayCells[row]);
            [_productCategory removeObjectAtIndex:row-1];
            [opitonsTableView.arrayCells removeObjectAtIndex:row];
            if (self.optionTableView1.arrayCells.count < 7) {
                CGRect frame = self.optionView1.frame;
                frame.size.height -= OptionsTableViewCellHeight;
                self.optionView1.frame = frame;
            }
            BLOCK_SAFE_ASY_RUN_MainQueue(completion, YES, nil);
        }
    }];
}
#pragma mark -- 代理方法
- (void)clickMengView:(ANOptionTableView *)optionTableView {
    _selectedBtn.imageView.transform = CGAffineTransformIdentity;
    _selectedBtn.selected = NO;
    //隐藏footView输入框
    [(ANOptionFootView*)_optionTableView.tableView.tableFooterView hideInputViewAnimation:YES];
}

#pragma mark --隐藏工具条
- (void)hiddenBar {
    _IsMoving = YES;
    UIViewController *vc = (UIViewController *)self.delegate;
    if (vc.navigationController.viewControllers.count > 1) return;
    _tableView.transform = CGAffineTransformMakeTranslation(0, -35);
    [UIView animateWithDuration:ANFindToolAnimationTime animations:^{
        vc.navigationController.navigationBar.layer.transform = CATransform3DMakeTranslation(0, -64, 0);
        self.layer.transform = CATransform3DMakeTranslation(0, -64, 0);
    } completion:^(BOOL finished) {
        vc.navigationController.navigationBar.alpha = 0;
        self.alpha = 0;
        _barIsHidden = YES;
        _IsMoving = NO;
    }];
}
- (void)recoveryBar {
    _IsMoving = YES;
    UIViewController *vc = (UIViewController *)self.delegate;
    self.alpha = 1;
    vc.navigationController.navigationBar.alpha = 1;
    [UIView animateWithDuration:ANFindToolAnimationTime animations:^{
        vc.navigationController.navigationBar.layer.transform = CATransform3DMakeTranslation(0, 0, 0);
        self.layer.transform = CATransform3DMakeTranslation(0, 0, 0);
    } completion:^(BOOL finished) {
        _barIsHidden = NO;
        _IsMoving = NO;
        _tableView.transform = CGAffineTransformMakeTranslation(0, 0);
    }];
}
- (ANFindToolBtn *)setUpOneButtonWithTitle:(NSString *)title {
    ANFindToolBtn *btn = [ANFindToolBtn buttonWithType:UIButtonTypeCustom];
    [btn setTitle:title forState:UIControlStateNormal];
    [btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [btn setImage:[UIImage imageNamed:@"timeline_icon_more_highlighted"] forState:UIControlStateSelected];
    [btn setImage:[UIImage imageNamed:@"timeline_icon_more"] forState:UIControlStateNormal];
    btn.titleLabel.font = [UIFont systemFontOfSize:ANFindToolFontSize];
    //设置间距
    btn.titleEdgeInsets = UIEdgeInsetsMake(0, 5, 0, 0);
    [self addSubview:btn];
    [self.btns addObject:btn];
    return btn;
}
- (void)layoutSubviews {
    [super layoutSubviews];
    //设置按钮frame
    CGFloat w = ANKeyWindow.bounds.size.width / self.btns.count;
    CGFloat h = self.height;
    CGFloat y = 0;
    for (int i = 0; i < self.btns.count; i++) {
        CGFloat x = i * w;
        UIButton *btn = self.btns[i];
        btn.tag = i + 100;
        btn.frame = CGRectMake(x, y, w, h);
//        if (i >= 1 && i < self.btns.count) {
//            UIImageView *divider = self.divides[i - 1];
//            divider.frame = CGRectMake(x, y, 1, h);
//        }
    }
    
    _toolbar.frame = self.bounds;
}

- (void)showCoverView{
    //蒙版视图
    UIButton *coverView = [[UIButton alloc] initWithFrame:CGRectMake(0, 99, ANKeyWindow.width, ANKeyWindow.height - 99)];
    coverView.backgroundColor = [UIColor blackColor];
    coverView.alpha = 0;
    [coverView addTarget:self action:@selector(clickCoverView) forControlEvents:UIControlEventTouchUpInside];
    _coverView = coverView;
    [UIView animateWithDuration:ANFindToolAnimationTime animations:^{
        coverView.alpha = 0.4;
    }];
    [self.superview insertSubview:coverView atIndex:1];
}
// 隐藏弹出菜单
- (void)clickCoverView {
    [self hideOptionView];
    [self hideCoverView];
}
//隐藏蒙版
- (void)hideCoverView {
    [UIView animateWithDuration:ANFindToolAnimationTime animations:^{
        _selectedBtn.imageView.transform = CGAffineTransformIdentity;
        _selectedBtn.selected = NO;
        _selectedBtn = nil;
        //隐藏footView输入框
        [(ANOptionFootView*)_optionTableView.tableView.tableFooterView hideInputViewAnimation:YES];
        self.coverView.alpha = 0;
    } completion:^(BOOL finished) {
        [self.coverView removeFromSuperview];
    }];
}
// 隐藏弹出菜单
- (void)hideOptionView {
        [UIView animateWithDuration:ANFindToolAnimationTime animations:^{
            _optionView.transform = CGAffineTransformIdentity;
            _optionView1.transform = CGAffineTransformIdentity;
            _optionView2.transform = CGAffineTransformIdentity;
        }completion:^(BOOL finished) {
            _optionView.alpha = 0;
            _optionView1.alpha = 0;
            _optionView2.alpha = 0;
        }];
 }

- (void)dealloc {
    ANLog(@"管理界面消失:%s",__func__);
}
@end
